import {Inject, InjectTable} from "../IOC/Inject"
import {ContactEntity} from '../models/Contact.entity';
import {getManager, Repository} from 'typeorm';
import {Context} from 'koa';
@InjectTable
export class ContactService {
    public async createContact(ctx: Context, data: any):Promise<Boolean> {
        const contactRepository:Repository<ContactEntity> = getManager().getRepository(ContactEntity);
        const contactData = contactRepository.create(data)
        try {
            await contactRepository.save(contactData)
            return true
        } catch (err) {
            console.log("创建失败--", err)
            return false
        }
    }

    public async findContact(query?:Object):Promise<ContactEntity[] | Boolean> {
        const contactRepository:Repository<ContactEntity> = getManager().getRepository(ContactEntity)
        try {
            return await contactRepository.find();
        } catch (err) {
            console.log("查找失败--", err)
            return false
        }
    }
}